package com.docker.sys.dic;

import com.docker.core.constant.MainConstant;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.redis.RedisConstant;
import kit.docker.redis.RedisKit;

import java.util.HashMap;
import java.util.Map;

public class DicRedis {

    public static String getCacheName() {
        return MainConstant.REDIS_DOCKER;
    }

    public static String buildKey(String key) {
        return RedisConstant.SYS_DIC + key;
    }

    public static boolean vali(String dicTypeCode, String value) {
        return RedisKit.hexists(getCacheName(), buildKey(dicTypeCode), value);
    }

    public static void save(Record dic) {
        Map<String, String> child = new HashMap<>();
        child.put(dic.getStr("dic_code"), dic.getStr("dic_name"));
        RedisKit.hmset(getCacheName(), buildKey(dic.getStr("dic_pcode")), child);
    }

    public static void hDel(String key, String field) {
        RedisKit.hdel(getCacheName(), buildKey(key), field);
    }

    public static void del() {
        RedisKit.delByPattern(getCacheName(), RedisConstant.SYS_DIC + "*");
    }

    public static Map<String, String> child(String key) {
        return RedisKit.hgetAll(getCacheName(), buildKey(key));
    }

}
